package com.geostar.constructionland.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geostar.constructionland.index.entity.InxStorageAcotEntity;
import com.geostar.constructionland.index.entity.model.IndexSupplementModel;
import com.geostar.constructionland.index.entity.vo.IndexSupplementListData;
import com.geostar.constructionland.index.mapper.IndexSupplementMapper;
import com.geostar.constructionland.index.service.IndexSupplementService;
import com.geostar.constructionland.index.util.FormFieldsUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

@Service
public class IndexSupplementImpl implements IndexSupplementService {

    @Resource
    IndexSupplementMapper indexSupplementMapper;

    @Override
    public IPage<IndexSupplementListData> queryIndexSupplementList(IndexSupplementModel indexSupplementModel) {
        Page<IndexSupplementListData> page = new Page<>(indexSupplementModel.getPagenum(), indexSupplementModel.getPagesize());
        QueryWrapper<IndexSupplementListData> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(indexSupplementModel.getDistCodeProvince())) {
            queryWrapper.eq("xzqhdm_province", indexSupplementModel.getDistCodeProvince());
        }
        if (StringUtils.isNotBlank(indexSupplementModel.getDistCodeCity())) {
            queryWrapper.eq("xzqhdm_city", indexSupplementModel.getDistCodeCity());
        }
        if (StringUtils.isNotBlank(indexSupplementModel.getDistCodeCountry())) {
            queryWrapper.eq("xzqhdm_country", indexSupplementModel.getDistCodeCountry());
        }
        if (StringUtils.isNotBlank(indexSupplementModel.getIndexYear())) {
            queryWrapper.eq("zbnf", indexSupplementModel.getIndexYear());
        }
        // 默认条件
        queryWrapper.eq("zblx", "ZBTZ");

        if (StringUtils.isNotBlank(indexSupplementModel.getSortName())) {
            Set<String> keySet = new HashSet<>();
            // 定义修改的值(后期可做成动态)
            keySet.add(indexSupplementModel.getSortName());
            //反射获取实体类注解上配置的数据表字段名
            HashMap<String, String> conditionMap = FormFieldsUtils.getProxyPojoValue(new InxStorageAcotEntity(), keySet);
            if (StringUtils.isNotBlank(indexSupplementModel.getSortOrder())) {
                if (indexSupplementModel.getSortOrder().equals("desc") ? true : false) {
                    queryWrapper.orderByDesc(conditionMap.get(indexSupplementModel.getSortName()));
                } else {
                    queryWrapper.orderByAsc(conditionMap.get(indexSupplementModel.getSortName()));
                }
            }

        }
//        return indexSupplementMapper.queryIndexSupplementListByWrapper(new Page<>(indexSupplementModel.getPagenum(), indexSupplementModel.getPagesize()),
//                queryWrapper);
        return null;
    }

}
